package youleche.com.controler.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.mysql.jdbc.UpdatableResultSet;

import youleche.com.business.BVehicleInfo;
import youleche.com.caches.CacheFactory;
import youleche.com.common.ParamConfigParameters;
import youleche.com.controler.servlet.common.UlecheHttpServlet;
import youleche.com.factory.MLLogFactory;
import youleche.com.model.MVehicleInfo;
import youleche.com.util.AESEncryptAndDecrypt;
import youleche.com.util.JsonHelper;

/**
 * Servlet implementation class CVehicleServletAuthority
 */
public class CVehicleServletAuthority extends UlecheHttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CVehicleServletAuthority() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		int action = Integer.parseInt(request.getParameter("action"));
		switch(action){
			case 0 :	
				//置顶车辆信息	
				this.topVehicleInfoById(request, response);
				break;
			case 1 :
				//根据ID获取车辆信息
				this.getVehicleInfoById(request, response);
				break;
		}
	}
	
	/**
	 * 根据ID获取车辆信息
	 * @param request
	 * @param response
	 */
	private void getVehicleInfoById(HttpServletRequest request, HttpServletResponse response){
		BVehicleInfo handler = new BVehicleInfo();
		
		try {
			MVehicleInfo info = handler.getVehicleInfoById(request.getParameter("id"));
			ParamConfigParameters paramConfig = new ParamConfigParameters();
			String key = paramConfig.getParameter("LinkNum_KEY");
			info.setLinkPhoneNum(AESEncryptAndDecrypt.decrypt(info.getLinkPhoneNum(), key));
			Gson gson = new Gson();
			super.response(response, gson.toJson(info));
		} catch (Exception e) {
			super.response(response, "");
			MLLogFactory.exceptionLog("根据ID获取车辆信息发生异常", "根据ID获取车辆信息发生异常", e.getMessage(), request.getRemoteAddr());
		}
	}
	
	/**
	 * 置顶车辆信息
	 * @param request
	 * @param response
	 */
	@SuppressWarnings("static-access")
	private void topVehicleInfoById(HttpServletRequest request, HttpServletResponse response){
		String vehicleId = request.getParameter("vehicleId");
		BVehicleInfo handler = new BVehicleInfo();
		try {
			if(handler.topVehicleInfoById(vehicleId) >0){
				CacheFactory.getVehicleCache().updateCache();
				super.response(response, "1");
				return;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		super.response(response, "0");
	}

}
